/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lvyh.lightframe.tracer.sample.dubbo.controller;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.utils.ReferenceConfigCache;
import com.alibaba.dubbo.rpc.service.GenericService;
import com.lvyh.lightframe.tracer.core.common.TraceContants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class DemoController {

    private static final Logger logger = LoggerFactory.getLogger(DemoController.class);
    @Autowired
    private Environment environment;

    // http://localhost:8089/dubbo-trace
    @RequestMapping(value = "dubbo-trace")
    @ResponseBody
    public String dubboTest() throws Exception {

        Map<String, Object> result = this.helloTest("lvyh", 1001);

        logger.info("[DemoController]业务执行开始");
        return "Send Message To B,result= " + result;
    }


    public Map<String, Object> helloTest(String userName, Integer id) {
        ApplicationConfig application = new ApplicationConfig();
        application.setName("light-frame-tracer-sample-dubbo");

        RegistryConfig registry = new RegistryConfig();
        registry.setAddress(environment.getProperty("dubbo.registry.address"));

        ReferenceConfig<GenericService> reference = new ReferenceConfig<>();
        reference.setApplication(application);
        reference.setRegistry(registry);
        reference.setInterface("com.lvyh.lightframe.tracer.sample.dubbo.provider.service.UserInfoService");
        reference.setGeneric(true);
        reference.setFilter(TraceContants.DUBBO_TRACER_FILTER_CONSUMER);
        reference.setTimeout(20000);

        ReferenceConfigCache cache = ReferenceConfigCache.getCache();
        GenericService genericService = cache.get(reference);
        String[] parameterTypes = new String[]{"com.lvyh.lightframe.tracer.sample.dubbo.provider.service.dto.UserInfoRequest"};

        Map params = new HashMap();
        params.put("userName", userName);
        params.put("id", id);

        Object result = genericService.$invoke("hello", parameterTypes, new Object[]{params});
        if (result != null) {
            Map<String, Object> resultMap = (HashMap) result;
            return resultMap;
        }

        return null;
    }

}
